import type { Meta, StoryObj } from "@storybook/react";
import { fn } from "@storybook/test";
import "../../styles/index.scss";
import { library } from "@fortawesome/fontawesome-svg-core";
import { fas } from "@fortawesome/free-solid-svg-icons";
import Progress from "./progress";
import { action } from "@storybook/addon-actions";
import { log } from "console";
import Button from "../Button/Button";
import { useEffect, useState } from "react";
library.add(fas);
// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
const meta = {
  title: "components/Progress",
  component: Progress,
  parameters: {
    // Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
    layout: "centered",
  },
  // This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
  tags: ["autodocs"],
  // More on argTypes: https://storybook.js.org/docs/api/argtypes
  argTypes: {},
  // Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#action-args
  args: {},
} satisfies Meta<typeof Progress>;

export default meta;
type Story = StoryObj<typeof meta>;
const checkFileSize = (file: File) => {
  if (Math.round(file.size / 1024) > 50) {
    alert("file is too large");
    return false;
  }
  return true;
};
const filePromise = (file: File) => {
  const newFile = new File([file], "new_name.docx", {
    type: file.type,
  });
  return Promise.resolve(newFile);
};

export const DefaultProgress: Story = {
  args: {
    percent: 0,
  },
  render: (args) => {
    const [percent, setPercent] = useState(args.percent);
    let timer: any = null;

    useEffect(() => {
      if (percent >= 100) {
        clearInterval(timer);
        return () => setPercent(() => 0);
      }
    }, [percent]);

    useEffect(() => {
      timer = setInterval(() => {
        setPercent((cur) => cur + 10);
      }, 1000);

      // 清除定时器以避免内存泄漏
      return () => clearInterval(timer);
    }, []);

    return (
      <div style={{ width: "500px" }}>
        <Progress percent={percent}></Progress>
      </div>
    );
  },
};
